package com.dong.web.database;

import com.dong.web.util.C3p0Util;
import com.dong.web.whold.Variables;

public final class Connected {

	private static ThreadLocal<JdbcHandler> jdbcThreadLocal = new ThreadLocal<JdbcHandler>();

	public static void clearThreadLocal() {
		if (jdbcThreadLocal.get() != null) {
			JdbcHandler jdbcHandler = jdbcThreadLocal.get();
			jdbcHandler.closeConnection();
			// jdbcHandler.close();
			jdbcThreadLocal.remove();
		}
	}

	public static JdbcHandler getJdbcHandler(String key) {
		if (jdbcThreadLocal.get() == null) {
			jdbcThreadLocal.set(new JdbcHandler(C3p0Util.getConnection(key)));
		}
		return jdbcThreadLocal.get();
	}

	public static JdbcHandler getJdbcHandler() {
		return getJdbcHandler(Variables.DEFAULT);
	}

}
